/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pos.system.standard.ui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Event;
import java.awt.FocusTraversalPolicy;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
 import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import pos.system.Role;
import pos.system.database.DBCreator;
import pos.system.standard.ui.util.GuiUtil;

/**
 *
 * @author Shameera
 */
public class StartWindow extends javax.swing.JFrame {

    private final boolean process;

    /**
     * Creates new form StartWindow
     */
    public StartWindow() {
        initComponents();
        initKeyBinding();
        this.setLocationRelativeTo(this);
        DBCreator creator = new DBCreator();
        creator.create();
        if (creator.isIsDBCreated() || creator.isIsDatabaseExist()) {
            process = true;
        } else {
            process = false;
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        startwindow_panel = new javax.swing.JPanel();
        startwindow_managerButton = new javax.swing.JButton();
        startwindow_adminButton = new javax.swing.JButton();
        startwindow_employeeButton = new javax.swing.JButton();
        startwindow_closeButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        startwindow_panel.setBackground(new java.awt.Color(187, 187, 105));

        startwindow_managerButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/user_male.png"))); // NOI18N
        startwindow_managerButton.setMnemonic('M');
        startwindow_managerButton.setText("Manager");
        startwindow_managerButton.setToolTipText("Manager");
        startwindow_managerButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        startwindow_managerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startwindow_managerButtonActionPerformed(evt);
            }
        });
        startwindow_managerButton.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                startwindow_managerButtonFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                startwindow_managerButtonFocusLost(evt);
            }
        });

        startwindow_adminButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/user_red.png"))); // NOI18N
        startwindow_adminButton.setMnemonic('A');
        startwindow_adminButton.setText("Adminstrator");
        startwindow_adminButton.setToolTipText("Adminstrator");
        startwindow_adminButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        startwindow_adminButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startwindow_adminButtonActionPerformed(evt);
            }
        });
        startwindow_adminButton.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                startwindow_adminButtonFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                startwindow_adminButtonFocusLost(evt);
            }
        });

        startwindow_employeeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/group.png"))); // NOI18N
        startwindow_employeeButton.setMnemonic('E');
        startwindow_employeeButton.setText("Employee");
        startwindow_employeeButton.setToolTipText("Employee");
        startwindow_employeeButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        startwindow_employeeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startwindow_employeeButtonActionPerformed(evt);
            }
        });
        startwindow_employeeButton.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                startwindow_employeeButtonFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                startwindow_employeeButtonFocusLost(evt);
            }
        });

        startwindow_closeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/button_cancel.png"))); // NOI18N
        startwindow_closeButton.setMnemonic('C');
        startwindow_closeButton.setText("Close");
        startwindow_closeButton.setToolTipText("Close Window");
        startwindow_closeButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        startwindow_closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startwindow_closeButtonActionPerformed(evt);
            }
        });
        startwindow_closeButton.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                startwindow_closeButtonFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                startwindow_closeButtonFocusLost(evt);
            }
        });

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/leafpos_logo.jpg"))); // NOI18N
        jLabel1.setIconTextGap(8);

        javax.swing.GroupLayout startwindow_panelLayout = new javax.swing.GroupLayout(startwindow_panel);
        startwindow_panel.setLayout(startwindow_panelLayout);
        startwindow_panelLayout.setHorizontalGroup(
            startwindow_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(startwindow_panelLayout.createSequentialGroup()
                .addGroup(startwindow_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(startwindow_panelLayout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(startwindow_adminButton, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(startwindow_managerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(startwindow_employeeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(startwindow_closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(startwindow_panelLayout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 9, Short.MAX_VALUE))
        );
        startwindow_panelLayout.setVerticalGroup(
            startwindow_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, startwindow_panelLayout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addGroup(startwindow_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(startwindow_adminButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(startwindow_managerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(startwindow_employeeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(startwindow_closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(startwindow_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(startwindow_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 1, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void startwindow_managerButtonFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_startwindow_managerButtonFocusGained
        GuiUtil.buttonFocusGained(startwindow_managerButton);
    }//GEN-LAST:event_startwindow_managerButtonFocusGained

    private void startwindow_adminButtonFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_startwindow_adminButtonFocusGained
        GuiUtil.buttonFocusGained(startwindow_adminButton);
    }//GEN-LAST:event_startwindow_adminButtonFocusGained

    private void startwindow_managerButtonFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_startwindow_managerButtonFocusLost
        GuiUtil.buttonFocusLost(startwindow_managerButton);        // TODO add your handling code here:
    }//GEN-LAST:event_startwindow_managerButtonFocusLost

    private void startwindow_adminButtonFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_startwindow_adminButtonFocusLost
        GuiUtil.buttonFocusLost(startwindow_adminButton);        // TODO add your handling code here:
    }//GEN-LAST:event_startwindow_adminButtonFocusLost

    private void startwindow_employeeButtonFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_startwindow_employeeButtonFocusGained
        GuiUtil.buttonFocusGained(startwindow_employeeButton);        // TODO add your handling code here:
    }//GEN-LAST:event_startwindow_employeeButtonFocusGained

    private void startwindow_employeeButtonFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_startwindow_employeeButtonFocusLost
        GuiUtil.buttonFocusLost(startwindow_employeeButton);        // TODO add your handling code here:
    }//GEN-LAST:event_startwindow_employeeButtonFocusLost

    private void startwindow_closeButtonFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_startwindow_closeButtonFocusGained
        GuiUtil.buttonFocusGained(startwindow_closeButton);        // TODO add your handling code here:
    }//GEN-LAST:event_startwindow_closeButtonFocusGained

    private void startwindow_closeButtonFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_startwindow_closeButtonFocusLost
        GuiUtil.buttonFocusLost(startwindow_closeButton);        // TODO add your handling code here:
    }//GEN-LAST:event_startwindow_closeButtonFocusLost

    private void startwindow_adminButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startwindow_adminButtonActionPerformed
//        pBag.addProperty(Constants.USER_TYPE, Role.ADMINSTRATOR);   
        showLoginWindow(Role.ADMINSTRATOR);// TODO add your handling code here:
    }//GEN-LAST:event_startwindow_adminButtonActionPerformed

    private void startwindow_managerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startwindow_managerButtonActionPerformed
//        pBag.addProperty(Constants.USER_TYPE, Role.MANAGER);  
        showLoginWindow(Role.MANAGER);// TODO add your handling code here:
    }//GEN-LAST:event_startwindow_managerButtonActionPerformed

    private void startwindow_employeeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startwindow_employeeButtonActionPerformed
//        pBag.addProperty(Constants.USER_TYPE, Role.EMPLOYEE);     
        showLoginWindow(Role.EMPLOYEE);// TODO add your handling code here:
    }//GEN-LAST:event_startwindow_employeeButtonActionPerformed

    private void startwindow_closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startwindow_closeButtonActionPerformed
        this.dispose();        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_startwindow_closeButtonActionPerformed

    private void showLoginWindow(Role role) {
        if (process) {
            this.dispose();
            new LoginWindow(role).setVisible(true);
        } else {
            int ret = JOptionPane.showConfirmDialog(this, "Cant process without the system database please try again", "Error Database absence", JOptionPane.OK_CANCEL_OPTION);
            System.exit(0);
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StartWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StartWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StartWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StartWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StartWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton startwindow_adminButton;
    private javax.swing.JButton startwindow_closeButton;
    private javax.swing.JButton startwindow_employeeButton;
    private javax.swing.JButton startwindow_managerButton;
    private javax.swing.JPanel startwindow_panel;
    // End of variables declaration//GEN-END:variables

    public FocusTraversalPolicy setFocustraversalPolicy() {
        return new FocusTraversalPolicy() {
            @Override
            public Component getComponentAfter(Container aContainer, Component aComponent) {
                if (aComponent.equals(startwindow_adminButton)) {
                    return startwindow_managerButton;
                } else if (aComponent.equals(startwindow_managerButton)) {
                    return startwindow_employeeButton;
                } else if (aComponent.equals(startwindow_employeeButton)) {
                    return startwindow_closeButton;
                } else if (aComponent.equals(startwindow_closeButton)) {
                    return startwindow_adminButton;
                }

                return aComponent;
            }

            @Override
            public Component getComponentBefore(Container aContainer, Component aComponent) {
                if (aComponent.equals(startwindow_managerButton)) {
                    return startwindow_adminButton;
                } else if (aComponent.equals(startwindow_employeeButton)) {
                    return startwindow_managerButton;
                } else if (aComponent.equals(startwindow_closeButton)) {
                    return startwindow_employeeButton;
                } else if (aComponent.equals(startwindow_adminButton)) {
                    return startwindow_closeButton;
                }

                return aComponent;
            }

            @Override
            public Component getFirstComponent(Container aContainer) {
                return startwindow_adminButton;
            }

            @Override
            public Component getLastComponent(Container aContainer) {
                return startwindow_closeButton;
            }

            @Override
            public Component getDefaultComponent(Container aContainer) {
                return startwindow_adminButton;
            }
        };
    }

    private void initKeyBinding() {
        String admin = "admin";
        startwindow_adminButton.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW)
                .put(KeyStroke.getKeyStroke(KeyEvent.VK_A, Event.CTRL_MASK), admin);
        startwindow_adminButton.getActionMap().put(admin, new AdminKeyAction());
        String manager = "manager";
        startwindow_managerButton.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW)
                .put(KeyStroke.getKeyStroke(KeyEvent.VK_M, Event.CTRL_MASK), manager);
        startwindow_managerButton.getActionMap().put(manager, new ManagerKeyAction());
        String employee = "employee";
        startwindow_employeeButton.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW)
                .put(KeyStroke.getKeyStroke(KeyEvent.VK_E, Event.CTRL_MASK), employee);
        startwindow_employeeButton.getActionMap().put(employee, new EmployeeKeyAction());
        String close = "close";
        startwindow_closeButton.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW)
                .put(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK), close);
        startwindow_closeButton.getActionMap().put(close, new CloseKeyAction());
    }

    private class AdminKeyAction extends AbstractAction {

        public AdminKeyAction() {
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            startwindow_adminButtonActionPerformed(e);
        }
    }

    private class ManagerKeyAction extends AbstractAction {

        public ManagerKeyAction() {
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            startwindow_managerButtonActionPerformed(e);
        }
    }

    private class EmployeeKeyAction extends AbstractAction {

        public EmployeeKeyAction() {
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            startwindow_employeeButtonActionPerformed(e);
        }
    }

    private class CloseKeyAction extends AbstractAction {

        public CloseKeyAction() {
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            startwindow_closeButtonActionPerformed(e);
        }
    }
}
